# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=pip

pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgname=("${MINGW_PACKAGE_PREFIX}-python2-pip" "${MINGW_PACKAGE_PREFIX}-python3-pip")
pkgver=1.5.6
pkgrel=1
pkgdesc="An easy_install replacement for installing pypi python packages (mingw-w64)"
arch=('any')
license=('MIT')
url="http://www.pip-installer.org/"
depends=("${MINGW_PACKAGE_PREFIX}-python2"
        "${MINGW_PACKAGE_PREFIX}-python2-setuptools"
        "${MINGW_PACKAGE_PREFIX}-python3"
        "${MINGW_PACKAGE_PREFIX}-python3-setuptools")
source=(http://pypi.python.org/packages/source/p/pip/${_realname}-${pkgver}.tar.gz)
md5sums=('01026f87978932060cc86c1dc527903e')
 
prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  #patch -p1 -i ${srcdir}/mingw-python-fix.patch

  cd "${srcdir}"

  cp -r ${_realname}-${pkgver} ${_realname}-python2
  cp -r ${_realname}-${pkgver} ${_realname}-python3
}

build() {
   # Build python 3 module
   cd "${srcdir}"/${_realname}-python3
   ${MINGW_PREFIX}/bin/python3 setup.py build
 
   # Build python 2 module
   cd "${srcdir}"/${_realname}-python2
   ${MINGW_PREFIX}/bin/python2 setup.py build
}

package_python2-pip() {
  depends=("${MINGW_PACKAGE_PREFIX}-python2" "${MINGW_PACKAGE_PREFIX}-python2-setuptools")
  
  pushd ${MINGW_PREFIX} > /dev/null
  local _mingw_prefix=`pwd -W`
  popd > /dev/null
  
  cd "${srcdir}/${_realname}-python2"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
    ${MINGW_PREFIX}/bin/python2 setup.py install --prefix=${MINGW_PREFIX#\/} --root="${pkgdir}" --optimize=1 --skip-build
  
  install -D -m644 LICENSE.txt "${pkgdir}${MINGW_PREFIX}/share/licenses/python2-${_realname}/LICENSE"
  for _ff in ${pkgdir}${MINGW_PREFIX}/bin/*.py; do
    sed -e "s|${_mingw_prefix}|${MINGW_PREFIX}|g" -i ${_ff}
  done
}
package_python3-pip() {
  depends=("${MINGW_PACKAGE_PREFIX}-python3" "${MINGW_PACKAGE_PREFIX}-python3-setuptools")

  pushd ${MINGW_PREFIX} > /dev/null
  local _mingw_prefix=`pwd -W`
  popd > /dev/null

  cd "${srcdir}/${_realname}-python3"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
    ${MINGW_PREFIX}/bin/python3 setup.py install --prefix=${MINGW_PREFIX#\/} --root="${pkgdir}" --optimize=1 --skip-build

  # Remove conflicted files
  rm -f ${pkgdir}${MINGW_PREFIX}/bin/pip{.exe,.exe.manifest,-script.py}
  
  for _ff in ${pkgdir}${MINGW_PREFIX}/bin/*.py; do
    sed -e "s|${_mingw_prefix}|${MINGW_PREFIX}|g" -i ${_ff}
  done

  install -D -m644 LICENSE.txt "${pkgdir}${MINGW_PREFIX}/share/licenses/python3-${_realname}/LICENSE"
}

package_mingw-w64-i686-python2-pip() {
  install=${_realname}2-${CARCH}.install
  package_python2-pip
}

package_mingw-w64-i686-python3-pip() {
  install=${_realname}3-${CARCH}.install
  package_python3-pip
}

package_mingw-w64-x86_64-python2-pip() {
  install=${_realname}2-${CARCH}.install
  package_python2-pip
}

package_mingw-w64-x86_64-python3-pip() {
  install=${_realname}3-${CARCH}.install
  package_python3-pip
}
